package xiaojian.toolkit.netty.handler.codec.http.transformer;

import org.apache.commons.collections4.Transformer;

import xiaojian.toolkit.ebus.EventBus;
import io.netty.handler.codec.http.FullHttpRequest;

public class FireEventTransformer implements HttpRequestTransformer {

	private String event;
	private EventBus eventBus;

	/**
	 * @return the event
	 */
	public String getEvent() {
		return event;
	}

	/**
	 * @param event
	 *            the event to set
	 */
	public void setEvent(String event) {
		this.event = event;
	}

	/**
	 * @return the eventBus
	 */
	public EventBus getEventBus() {
		return eventBus;
	}

	/**
	 * @param eventBus
	 *            the eventBus to set
	 */
	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	/**
	 * @return the impl
	 */
	public Transformer getImpl() {
		return impl;
	}

	/**
	 * @param impl
	 *            the impl to set
	 */
	public void setImpl(Transformer impl) {
		this.impl = impl;
	}

	private Transformer impl;

	@Override
	public Object transform(FullHttpRequest from) {
		Object bean = impl.transform(from);

		if (null != bean) {
			eventBus.fireEvent(event, bean);
		}
		return bean;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "event:" + event;
	}

}
